package wqh.controller;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.DtMonitor;
import wqh.domain.DtDevice;
import wqh.domain.DtGeoBuilding;
import wqh.domain.DtMapPos;
import wqh.pojo.*;
import wqh.service.DtMonitorService;
import wqh.service.DtDeviceService;
import wqh.service.DtGeoBuildingService;
import wqh.service.DtMapPosService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by cfs on 2016/12/26.
 */

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/dt_monitor")
public class DtMonitorJSONController {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(DtMonitorJSONController.class);

    @Autowired
    private DtMonitorService dtMonitorService;
    @Autowired
    private DtDeviceService dtDeviceService;
    @Autowired
    private DtGeoBuildingService dtGeoBuildingService;
    @Autowired
    private DtMapPosService dtMapPosService;

    @RequestMapping(value = "getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        List<DtMonitor> dtMonitorList = dtMonitorService.findAllRecord();
        for(DtMonitor dtMonitor : dtMonitorList)
        {
            DtDevice dtDevice = dtDeviceService.findAllById(dtMonitor.getDt_device_id());
            if(dtDevice!=null)
            {
                dtMonitor.setDt_device_name(dtDevice.getName());
                dtMonitor.setIp(dtDevice.getIp());
            }
            else
            {
                dtMonitor.setDt_device_name("监测点主机异常");
            }
            String geo = "无地理信息";
            if(dtMonitor.getGeo_id() != 0){
                DtGeoBuilding gA = dtGeoBuildingService.findRecordById(dtMonitor.getGeo_id());
                if(gA == null){
                    dtMonitor.setGeo(geo);
                    dtMonitorService.updateRecord(dtMonitor);
                    continue;
                }
                geo = gA.getName();
                if(gA.getLevel() == 2){
                    DtGeoBuilding gB = dtGeoBuildingService.findRecordById(gA.getParent_id());
                    if(gB != null){
                        geo = gB.getName() + "_" + geo;
                    }else {
                        dtMonitor.setGeo(geo);
                        dtMonitorService.updateRecord(dtMonitor);
                    }
                }
                if(gA.getLevel() == 3){
                    DtGeoBuilding gB = dtGeoBuildingService.findRecordById(gA.getParent_id());
                    if(gB != null)
                    {
                        geo = gB.getName() + "_" + geo;
                        DtGeoBuilding gC = dtGeoBuildingService.findRecordById(gB.getParent_id());
                        if(gC != null)
                        {
                            geo = gC.getName() + "_"+ geo;
                        }
                        else
                        {
                            dtMonitor.setGeo(geo);
                            dtMonitorService.updateRecord(dtMonitor);
                        }
                    }
                    else
                    {
                        dtMonitor.setGeo(geo);
                        dtMonitorService.updateRecord(dtMonitor);
                    }
                }
            }
            dtMonitor.setGeo(geo);
            dtMonitorService.updateRecord(dtMonitor);
        }
        return MapRet.DATA(dtMonitorList);
    }

    @RequestMapping(value = "getAllByGeoId",method = RequestMethod.GET)
    public Object getAllByGeoId(@RequestParam(value = "geo_id",required = true) Integer geo_id)
    {
        try{
            List<DtMonitor> dtMonitorList = dtMonitorService.findByGeoID(geo_id);
            return MapRet.DATA(dtMonitorList);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.ERROR();
        }
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody DtMonitor dtMonitor)
    {
        SelectMonitorCondition selectMonitorCondition = new SelectMonitorCondition(dtMonitor.getIp(),dtMonitor.getMarkID());
        DtMonitor oldDtMonitor = dtMonitorService.findByIPAndMarkID(selectMonitorCondition);
        if(oldDtMonitor != null) return MapRet.SQL_INSERT_ERROR2();
        try {
            dtMonitor.setId(null);
            dtMonitorService.insertRecord(dtMonitor);
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            System.out.println(e);
            e.printStackTrace();
            return MapRet.SQL_INSERT_ERROR();
        }


    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody DtMonitor dtMonitor)
    {
        if(dtMonitor.getId() == null) return MapRet.DATA_ERROR("id不存在");
        SelectMonitorCondition selectMonitorCondition = new SelectMonitorCondition(dtMonitor.getIp(),dtMonitor.getMarkID());
        DtMonitor oldDtMonitor = dtMonitorService.findByIPAndMarkID(selectMonitorCondition);
        if(oldDtMonitor != null && (oldDtMonitor.getId() != dtMonitor.getId())) return MapRet.SQL_INSERT_ERROR2();
        try {
            if(dtMonitorService.findByID(dtMonitor.getId()) == null)
            {
                return MapRet.SQL_ERROR("系统中无此记录");
            }else {
                dtMonitorService.updateRecord(dtMonitor);
                return MapRet.SQL_UPDATE_SUCCESS();
            }
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestParam(value = "id",required = true) int id)
    {
        try{
            dtMonitorService.deleteById(id);
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除监测点失败："+ e);
            return MapRet.SQL_ERROR(e);
        }
    }
}
